<template>
  <div class="swiper-container">
    <div class="swiper-wrapper">
      <div v-for="(item,index) in skuImgeList" :key="item.id" class="swiper-slide">
        <img @click="activeImg(index)" :src="item.imgUrl" />
      </div>
    </div>
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
  </div>
</template>

<script>
// 引入swiper样式
import 'swiper/swiper-bundle.min.css'
import 'swiper/swiper.min.css'
import 'swiper/components/navigation/navigation.min.css'
import 'swiper/components/pagination/pagination.min.css'

import Swiper, { Navigation, Pagination } from 'swiper';

Swiper.use([Navigation, Pagination]);

export default {
  name: "ImageList",
  props: ["skuImgeList"],
  data() {
    return {
      defaultIndex: 0,
    };
  },
  methods:{
    //点击缩略图切换样式 和 选中大图 的事件回调函数
    activeImg(index){
      this.defaultIndex=index

      //调用事件总线上的事件,向Zoom组件传值
      this.$bus.$emit('getZoomIndex',index)
    }
  },
  watch:{
    skuImgeList:{
      //保持深度监听 对一开始就有数据的就执行函数
      immediate: true,
      handler() {
        this.$nextTick(() => {
          if (!this.skuImgeList.length) return
          new Swiper(".swiper-container", {
            direction: "horizontal", // 水平切换选项
            // loop: true, // 循环模式选项

            //页面显示的数量
            slidesPerView:4,
            // 每次移动的数量
            slidesPerGroup:1,

            // 如果需要分页器
            pagination: {
              el: ".swiper-pagination",
            },

            // 如果需要前进后退按钮
            navigation: {
              nextEl: ".swiper-button-next",
              prevEl: ".swiper-button-prev",
            },
          });
        });
      },
    }
  }
};
</script>

<style lang="less" scoped>
.swiper-container {
  height: 56px;
  width: 412px;
  box-sizing: border-box;
  padding: 0 12px;

  .swiper-slide {
    width: 56px;
    height: 56px;

    img {
      width: 100%;
      height: 100%;
      border: 1px solid #ccc;
      padding: 2px;
      width: 50px;
      height: 50px;
      display: block;

      &.active {
        border: 2px solid #f60;
        padding: 1px;
      }

      &:hover {
        border: 2px solid #f60;
        padding: 1px;
      }
    }
  }

  .swiper-button-next {
    left: auto;
    right: 0;
  }

  .swiper-button-prev {
    left: 0;
    right: auto;
  }

  .swiper-button-next,
  .swiper-button-prev {
    box-sizing: border-box;
    width: 12px;
    height: 56px;
    background: rgb(235, 235, 235);
    border: 1px solid rgb(204, 204, 204);
    top: 0;
    margin-top: 0;
    &::after {
      font-size: 12px;
    }
  }
}
</style>